package cn.com.inossem.activity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.foxykeep.datadroid.requestmanager.Request;

import java.util.List;

import cn.com.inossem.base.RequestActivity;
import cn.com.inossem.model.QueryProjectInfoBean;
import cn.com.inossem.model.QueryProjectOpportunityInfoBean;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.ActivityStackControlUtil;
import cn.com.inossem.utils.Utils;
import cn.com.inossem.utils.view.EditNameDialogFragment;
import cn.com.inossem.utils.view.EditNameDialogFragment.LoginInputListener;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * 机会详情
 *
 * @author szc 2015-8-24
 * @since 1.0
 */
@RuntimePermissions
public class JobsDetailsActivity extends RequestActivity implements
		LoginInputListener {

	LinearLayout releasepeople, applyjob;
	RelativeLayout collection;// 收藏
	ImageView shaerall, back;
	TextView shenqing, projectName, requireNo, startDate, projectCycle,
			projectAddress, payMethod, impMethod, friendNum, projectIndustry,
			projectNature, modelRequire, position, require, stationDuty,
			projectComment;
	EditNameDialogFragment dialog = null;
	String id;

	// ListSerializable ls;
	QueryProjectInfoBean bean = null;
	TextView applyNum;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.jobsdetails_layout);

		// ls = (ListSerializable) getIntent().getSerializableExtra("list");
		// bean = JobsOpportunityFragment.qpib;
		id = getIntent().getStringExtra("id");
		System.out.println("id:" + id);
		initView();
		QueryProjectOpportunityInfoBean querybean = new QueryProjectOpportunityInfoBean();
		querybean.projectID = id;
		System.out.println("querybean.projectID:" + querybean.projectID);
		launchRequest(WooRequestFactory
				.getJobsOpportunityDataRequest(querybean));
	}

	@Override
	public void onDestroy() {
		ActivityStackControlUtil.remove(this);
		super.onDestroy();
	}

	private void initView() {
		back = (ImageView) this.findViewById(R.id.back);
		back.setOnClickListener(this);

		shaerall = (ImageView) this.findViewById(R.id.shaerall);
		shaerall.setOnClickListener(this);

		collection = (RelativeLayout) this.findViewById(R.id.collection);
		collection.setOnClickListener(this);

		releasepeople = (LinearLayout) this.findViewById(R.id.releasepeople);
		releasepeople.setOnClickListener(this);

		applyjob = (LinearLayout) this.findViewById(R.id.applyjob);
		applyjob.setOnClickListener(this);

		projectName = (TextView) this.findViewById(R.id.projectName);
		requireNo = (TextView) this.findViewById(R.id.requireNo);
		startDate = (TextView) this.findViewById(R.id.startDate);
		projectCycle = (TextView) this.findViewById(R.id.projectCycle);
		projectAddress = (TextView) this.findViewById(R.id.projectAddress);
		payMethod = (TextView) this.findViewById(R.id.payMethod);
		impMethod = (TextView) this.findViewById(R.id.impMethod);
		applyNum = (TextView) this.findViewById(R.id.applyNum);
		friendNum = (TextView) this.findViewById(R.id.friendNum);
		friendNum.setOnClickListener(this);
		projectIndustry = (TextView) this.findViewById(R.id.projectIndustry);
		projectNature = (TextView) this.findViewById(R.id.projectNature);
		modelRequire = (TextView) this.findViewById(R.id.modelRequire);
		position = (TextView) this.findViewById(R.id.position);
		require = (TextView) this.findViewById(R.id.require);
		stationDuty = (TextView) this.findViewById(R.id.stationDuty);
		projectComment = (TextView) this.findViewById(R.id.projectComment);
		shenqing = (TextView) this.findViewById(R.id.shenqing);

	}

	@Override
	public Request getInitialRequest() {
		return null;

	}

	public void setInfo() {
		if (bean == null)
			return;
		projectName.setText(bean.projectName);
		requireNo.setText("需求号 " + bean.requireNo);
		startDate.setText(bean.startDate + " 开始");
		projectCycle.setText(bean.projectCycle.equals("-99") ? "入职" : bean.projectCycle + "个月");
		projectAddress.setText(bean.projectAddress);
		payMethod.setText("付款方式： " + bean.payMethod);
		impMethod.setText("实施方式： " + bean.impMethod);
		applyNum.setText(bean.applyNum);
		friendNum.setText(bean.friendNum);
		projectIndustry.setText("行业： " + bean.projectIndustry);
		projectNature.setText("项目性质：" + bean.projectNature);
		modelRequire.setText("模块需求：" + bean.modelRequire);
		position.setText("职位：" + bean.station);
		require.setText("要求：" + bean.projectRequire);
		stationDuty.setText("岗位职责：" + bean.stationDuty);
		projectComment.setText(bean.projectComment);
		if (bean.status.equals("10991")) {
			shenqing.setText("取消申请该职位");
		} else if (bean.status.equals("10992")) {
			shenqing.setText("已加入该项目");
		}
	}

	@Override
	public void onRequestSucess(Request request, Bundle bundle) {
		System.out.println("-----------0---------------");
		if (bundle.containsKey(WooRequestFactory.BUNDLE_JOBSOPPORTUNITY_LIST)) {
			List<QueryProjectInfoBean> retList = bundle
					.getParcelableArrayList(WooRequestFactory.RESPONSE_JOBSOPPORTUNITY_DATA);
			if (retList.size() > 0) {
				bean = retList.get(0);
				setInfo();
			}
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_APPLYSUCCESS)) {
			System.out.println("-----------1---------------");
			Toast.makeText(this, "收藏成功！", Toast.LENGTH_LONG).show();
			bean.collect = "10994";
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_APPLYFAILURE)) {
			// System.out.println("-----------2---------------");
			// Toast.makeText(this, "已经收藏！", Toast.LENGTH_LONG).show();
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_APPLYQUXIAO)) {
			Toast.makeText(this, "取消收藏！", Toast.LENGTH_LONG).show();

		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_POSITIONSUCCESS)) {
			Toast.makeText(this, "申请成功！", Toast.LENGTH_LONG).show();

			bean.applyNum = (Integer.parseInt(bean.applyNum) + 1) + "";
			applyNum.setText(bean.applyNum);
			bean.status = "10991";
			shenqing.setText("取消申请该职位");
		} else if (bundle
				.containsKey(WooRequestFactory.BUNDLE_EXTRA_POSITIONQUXIAO)) {
			Toast.makeText(this, "取消申请！", Toast.LENGTH_LONG).show();

			bean.applyNum = (Integer.parseInt(bean.applyNum) - 1) + "";
			applyNum.setText(bean.applyNum);
			bean.status = "-1";
			shenqing.setText("申请该职位");
		}

	}

	@Override
	public void onRequestError(int exceptionType) {

	}

	public int getContentViewId() {
		return R.layout.jobsdetails_layout;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
			case R.id.back:
				setResult(99);
				finish();
				break;
			case R.id.shaerall:
				JobsDetailsActivityPermissionsDispatcher.startShareWithCheck(this);
				break;
			case R.id.collection:
				if (bean == null)
					return;
				if (Utils.isDenglu == 0) {
					Toast.makeText(this, "请登录", Toast.LENGTH_LONG).show();
					return;
				}
				System.out.println("bean.collect:" + bean.collect);
				if (bean.collect.equals("10994")) {
					Toast.makeText(this, "已经收藏了！", Toast.LENGTH_LONG).show();
				} else {
					Utils.type = 0;
					launchRequest(WooRequestFactory.getApplyCollect(bean));
				}
				break;
			case R.id.releasepeople:
				if (bean == null)
					return;
				if (Utils.isDenglu == 0) {
					Toast.makeText(this, "请登录", Toast.LENGTH_LONG).show();
					return;
				} else {
					// String mail = bean.publishMail;
					String[] email = {""}; // 需要注意，email必须以数组形式传入
					email[0] = bean.publishMail;
					System.out.println("email[0]:" + email[0]);
					intent = new Intent(Intent.ACTION_SEND);
					intent.setType("message/rfc822"); // 设置邮件格式
					intent.putExtra(Intent.EXTRA_EMAIL, email); // 接收人
					// intent.putExtra(Intent.EXTRA_CC, email); // 抄送人
					intent.putExtra(Intent.EXTRA_SUBJECT, "关于项目需求" + bean.requireNo
							+ bean.projectName + "的疑问"); // 主题
					intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<br/><br/><br/><br/><br/><br/>来自用户：" +
							Utils.messageInfo.userInfo.getUsername() + ","
							+ Utils.messageInfo.userInfo.getTruename()
							+ "<br/>邮件地址：" + Utils.messageInfo.userInfo.getEmail())); // 正文
					startActivity(Intent.createChooser(intent, "请选择邮件类应用"));
				}

				break;
			case R.id.applyjob:
				if (bean == null)
					return;
				if (Utils.isDenglu == 0) {
					Toast.makeText(this, "请登录", Toast.LENGTH_LONG).show();
					return;
				}
				// intent = new Intent(this, ReleaseDialogActivity.class);
				// startActivity(intent);
				if (bean.status.equals("10991")) {
					showDialog(COMMON_DIALOG);

				} else if (bean.status.equals("10992")) {
					return;
				} else {
					int type = 0;
					if (bean.projectCycle.equals("入职")) {
						type = 1;
					}
					dialog = new EditNameDialogFragment(type);
					dialog.show(getFragmentManager(), "loginDialog");
				}
				break;
			case R.id.friendNum:
				break;
			default:
				break;
		}
	}

	@NeedsPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
	public void startShare() {
		if (bean == null)
			return;
		Intent intent = new Intent(this, ShareDialogActivity.class);
		intent.putExtra("name", bean.projectName);
		intent.putExtra("id", id);
		intent.putExtra("startDate", bean.startDate);
		intent.putExtra("projectCycle", bean.projectCycle);
		intent.putExtra("projectAddress", bean.projectAddress);
		startActivity(intent);
	}

	final int COMMON_DIALOG = 1;

	protected Dialog onCreateDialog(int id) { // 重写onCreateDialog方法
		Dialog dialog = null; // 声明一个Dialog对象用于返回
		switch (id) { // 对id进行判断
			case COMMON_DIALOG:
				Builder b = new AlertDialog.Builder(this);
				// b.setIcon(); //设置对话框的图标
				b.setTitle(R.string.tishi); // 设置对话框的标题
				// b.setMessage(R.string.Ok); // 设置对话框的显示内容
				b.setPositiveButton( // 添加按钮
						R.string.Ok, new DialogInterface.OnClickListener() { // 为按钮添加监听器
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
								Utils.type = 3;
								launchRequest(WooRequestFactory
										.getApplyCollect(bean));
							}
						});
				b.setNegativeButton( // 添加按钮
						R.string.Cancel, new DialogInterface.OnClickListener() { // 为按钮添加监听器
							public void onClick(DialogInterface dialog, int which) {
								// TODO Auto-generated method stub
							}
						});
				dialog = b.create(); // 生成Dialog对象
				break;
			default:
				break;
		}
		return dialog; // 返回生成Dialog的对象
	}

	@Override
	public void onLoginInputComplete(String money, String beizhu) {
		if (!money.equals("")) {
			System.out.println("申请职位!");
			// launchRequest(WooRequestFactory.getApplyCollect(1, bean));
			Utils.type = 1;
			bean.salary = money;
			bean.remark = beizhu;

			Utils.money = money;
			Utils.beizhu = beizhu;

			System.out.println(bean.salary + bean.remark);
			launchRequest(WooRequestFactory.getApplyCollect(bean));
			System.out.println(bean.salary + bean.remark);
			System.out.println(bean.salary + bean.remark);
			if (dialog != null) {
				dialog.dismiss();
				dialog = null;
			}
		} else {
			Toast.makeText(this, "薪资不为空！", Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {

			setResult(99);
			finish();

			return true;

		}

		return super.onKeyDown(keyCode, event);

	}

}
